<?php 
require_once ("$basedir"."/Login/config.php");
$conn=mysql_connect($host,$user,$password);

if(!conn)
{
 $errorse="Sorry Unable to connect with Server!";
 $_SESSION['errorse']=$errorse;
}
mysql_select_db($db);
if(!mysql_select_db($db))
{
$errorse="Sorry Unable to connect with the database!";
 $_SESSION['errordb']=$errorse;
}
	  if ($_FILES["file"]["error"] > 0)
  {
 $_SESSION['error']="Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  $_SESSION['e1']="Upload: " . $_FILES["file"]["name"] . "<br />";
  $_SESSION['e2']="Type: " . $_FILES["file"]["type"] . "<br />";
  $_SESSION['e3']="Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  $_SESSION['e4']="Stored in: " . $_FILES["file"]["tmp_name"];
  }
if (file_exists("Upload/" . $_FILES["file"]["name"]))
      {
     $_SESSION['errorex']=$_FILES["file"]["name"] . " already exists.". "<br />";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "Upload/" . $_FILES["file"]["name"]);
     
      }

    $header = null; 
    $data = array(); 
    $csvData=str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
    $delimiter = ",";
    foreach($csvData as $csvLine){ 

if(is_null($header))
{ $header = explode($delimiter, $csvLine);
				
} 
        else{
            $sql="INSERT INTO exam(School_Id,Exam_Name,Year,No_Of_Sat_Students,No_Of_Passed_Students) VALUES(";
            $items = explode($delimiter, $csvLine); 
            
            for($n = 0, $m = count($header); $n < $m; $n++){ 
               
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
            } 
			$sql.= ")";
			echo '<br/>';
            mysql_query($sql);
          
        } 
    } 
	
 mysql_close($conn);
?> 